package hanelsoft.vn.dictionaylearn.control.custom;

import hanelsoft.vn.dictionaylearn.R;
import hanelsoft.vn.dictionaylearn.model.entity.InfoVocabularyContentDelete;

import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class CustomVocabularyContentDelete extends ArrayAdapter<InfoVocabularyContentDelete>{
	String Tag ="CustomVocabularyContentDelete";
	SharedPreferences share;
	Activity context;
	ArrayList<InfoVocabularyContentDelete> list;
	InfoVocabularyContentDelete info;
	public CustomVocabularyContentDelete(Activity context, ArrayList<InfoVocabularyContentDelete> objects) {
		super(context, R.layout.custom_vocabulary_row_delete, objects);
		this.context = context;
		this.list = objects;
	}
	class ViewHolder
	{
		TextView word;
		CheckBox cbDelete;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View rowView;;
		if(convertView == null)
		{
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.custom_vocabulary_row_delete, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.word = (TextView)rowView.findViewById(R.id.custom_vocabulary_row_delete);
			viewHolder.cbDelete = (CheckBox)rowView.findViewById(R.id.custom_vocabulary_row_delete_cb);
			rowView.setTag(viewHolder);
		}
		else
		{
			rowView = convertView;
		}
		share = context.getSharedPreferences("CheckDelete", 1);
		share.edit().putInt("PositionCheckItem"+position, -1).commit();
		final ViewHolder holder = (ViewHolder)rowView.getTag();
		info = list.get(position);
		holder.word.setText(info.getWord());
		holder.cbDelete.setChecked(false);
		holder.cbDelete.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(holder.cbDelete.isChecked())
				{
					share.edit().putInt("PositionCheckItem"+position, position).commit();
				}
				else
				{
					share.edit().remove("PositionCheckItem"+position).commit();
				}
			}
		});
		return rowView;
	}
}

